(function() {
    var a = wx.getSharedCanvas(), b = a.getContext("2d"), c = {
        FRIEND: 0,
        GROUP: 1,
        MINE: 2
    }, d = null, e = function(a, c) {
        console.log("showLoading _type:", a, d, b), b.font = c ? "25px serif" : "30px serif", 
        b.fillStyle = "yellow";
        let e = c ? -2 : 200, f = c ? 150 : 280;
        b.fillText("加载中...", e, f), d = a;
    };
    wx.onMessage(a => {
        console.log("game.js -> index.js :", a);
        var d = [];
        for (let b = 0; b < a.KVDataList.length; b++) d.push(a.KVDataList[b].key);
        var j = a.method, k = [], l = a.isSmall, m = l ? j + "_Small" : j;
        wx.setUserCloudStorage({
            KVDataList: a.KVDataList,
            success: function(b) {
                switch (console.log("game.js -> index.js setUserCloudStorage success:", b), j) {
                  case "showFriendRank_Small":
                  case "showFriendRank":
                    {
                        var d = c.FRIEND;
                        e(d, l), p();
                        break;
                    }

                  case "showGroupRank_Small":
                  case "showGroupRank":
                    {
                        var d = c.GROUP;
                        e(d, l);
                        var f = a.shareTicket;
                        n(f);
                        break;
                    }

                  case "showMyRank":
                    {
                        var d = c.MINE;
                        e(d, l), o();
                        break;
                    }

                  default:
                }
            },
            fail: function() {}
        });
        var n = function(a) {
            wx.getGroupCloudStorage({
                shareTicket: a,
                keyList: d,
                success: function(a) {
                    console.log("game.js -> index.js getGroupData success:", a), q(a);
                },
                fail: function() {}
            });
        }, o = function() {
            wx.getUserCloudStorage({
                keyList: d,
                success: function(a) {
                    console.log("game.js -> index.js getMyData success:", a);
                    for (var b = a.KVDataList, c = 0; c < b.length; c++) for (var d in b[c]) ;
                },
                fail: function() {}
            });
        }, p = function() {
            wx.getFriendCloudStorage({
                keyList: d,
                success: function(a) {
                    console.log("game.js -> index.js getFriData success:", a), q(a);
                },
                fail: function() {}
            });
        }, q = function(a) {
            l ? b.clearRect(-2, 0, 136, 370) : b.clearRect(0, 0, 537, 524);
            for (var c = a.data, d = 0; d < c.length; d++) for (var e in c[d]) if ("KVDataList" == e) {
                let a = c[d][e];
                k.push(parseInt(a[0].value));
            }
            k = g(k);
            let h = f(k, c);
            r(h);
        }, r = function(a) {
            let b = l ? 4 : 20;
            for (let c = 0; c < b; c++) l ? a[c] ? t(c, a[c]) : s(c) : a[c] ? v(c, a[c]) : s(c);
        }, s = function(a) {
            if (l) console.log("排行榜第" + a + "行没人"); else {
                var c = wx.createImage();
                c.src = "src/share/PHB_9_1.png", c.onload = function() {
                    b.drawImage(c, 11, 15 + 112 * a, 510, 97);
                    var d = wx.createImage();
                    d.src = "src/share/PHB_9.png", d.onload = function() {
                        b.drawImage(d, 7, 15 + 112 * a, 6, 97);
                    }.bind(a);
                    var e = wx.createImage();
                    e.src = "src/share/PHB_9_2.png", e.onload = function() {
                        b.drawImage(e, 521, 15 + 112 * a, 6, 97);
                    }.bind(a), u(a), b.font = "30px serif", b.fillStyle = "rgb(86,45,12)", b.fillText("虚位以待", 175, 72 + 112 * a);
                }.bind(a);
            }
        }, t = function(a, c) {
            var d = wx.createImage();
            d.src = "src/share/PHB_5.png", d.onload = function() {
                b.drawImage(d, 10, 10 + 100 * a, 90, 90);
                var e = wx.createImage();
                e.src = c.avatarUrl, e.onload = function() {
                    b.drawImage(e, 14, 14 + 100 * a, 81, 81);
                }.bind(a);
            }.bind(a);
        }, u = function(a) {
            b.font = "40px serif", b.fillStyle = "green";
            let c = 36;
            9 <= a && (c = 21), b.fillText(a + 1, c, 75 + 112 * a);
        }, v = function(a, c) {
            var d = wx.createImage();
            d.src = "src/share/PHB_9_1.png", d.onload = function() {
                b.drawImage(d, 11, 15 + 112 * a, 510, 97);
                var e = wx.createImage();
                e.src = "src/share/PHB_9.png", e.onload = function() {
                    b.drawImage(e, 7, 15 + 112 * a, 6, 97);
                }.bind(a);
                var f = wx.createImage();
                f.src = "src/share/PHB_9_2.png", f.onload = function() {
                    b.drawImage(f, 521, 15 + 112 * a, 6, 97);
                }.bind(a);
                var g = wx.createImage();
                if (g.src = c.avatarUrl, g.onload = function() {
                    b.drawImage(g, 82, 26 + 112 * a, 75, 75);
                }.bind(a), 3 > a) {
                    var j = wx.createImage();
                    j.src = "src/share/PHB_" + (a + 12) + ".png", j.onload = function() {
                        b.drawImage(j, 16, 36 + 112 * a, 56, 50);
                    }.bind(a);
                } else u(a);
                b.font = "30px serif", b.fillStyle = "rgb(86,45,12)", b.fillText(h(c.nickname, 4), 166, 73 + 112 * a), 
                b.font = "30px serif", b.fillStyle = "brown", b.fillText(i(c.KVDataList[0].value), 340, 73 + 112 * a), 
                b.font = "30px serif", b.fillStyle = "rgb(142,74,31)", b.fillText("金币", 450, 73 + 112 * a);
            }.bind(a);
        };
    });
    var f = function(a, b) {
        let c = [], d = {};
        for (var e = 0; e < a.length; e++) for (var f = 0; f < b.length; f++) d[f] || a[e] == b[f].KVDataList[0].value && (c.push(b[f]), 
        d[f] = 1);
        return c;
    }, g = function(a) {
        for (var b = 0; b < a.length - 1; b++) for (var c = 0; c < a.length - 1 - b; c++) if (a[c] < a[c + 1]) {
            var d = a[c];
            a[c] = a[c + 1], a[c + 1] = d;
        }
        return a;
    }, h = function(a, b) {
        if (!a || 0 > b || "string" != typeof a) return "";
        for (var c = 0, d = 0; d < a.length; d++) if (/[\u0000-\u00ff]/g.test(a.charAt(d)) ? c += .5 : /[\u4e00-\u9fa5]/g.test(a.charAt(d)) ? c += 1 : /[\uff00-\uffff]/g.test(a.charAt(d)) && (c += 1), 
        c > b) {
            a = a.slice(0, d - 1) + "...";
            break;
        }
        return a;
    }, i = function(a) {
        var b = a;
        0 > b && (b = -b);
        var c = "";
        if (99999999 < b) {
            var d = b / 1e8;
            return c = j(d), 0 > a && (c = "-" + c), c + "亿";
        }
        if (9999 < b) {
            var d = b / 1e4;
            return c = j(d), 0 > a && (c = "-" + c), c + "万";
        }
        return a + "";
    }, j = function(a) {
        var b = a.toString(), c = b.split(".");
        if (1 == c.length) return c[0];
        var d = 4 - c[0].length;
        if (0 >= d) return c[0];
        if (2 == d) {
            var e = c[1].substr(0, 1), f = c[1].substr(1, 1);
            return "0" == f ? "0" == e ? c[0] : c[0] + "." + e : c[0] + "." + e + f;
        }
        if (1 == d) {
            var e = c[1].substr(0, 1);
            return "0" == e ? c[0] : c[0] + "." + e;
        }
        if (3 == d) {
            var e = c[1].substr(0, 1), f = c[1].substr(1, 1), g = c[1].substr(2, 1);
            return "0" == g ? "0" == f ? "0" == e ? c[0] : c[0] + "." + e : c[0] + "." + e + f : c[0] + "." + e + f + g;
        }
    };
})();